/**
 * 标记工具
 */
class DrawMarker {
  viewer: any;
  MarkerSource: any;

  handlerMarker: any;
  markerPoints: any;
  constructor(viewer: any, MarkerSource: any) {
    this.viewer = viewer;
    this.MarkerSource = MarkerSource;
  }

  // 绘制程序的准备工作，通过其他方式触发activate
  readyDrawMarkerHandler() {
    this.handlerMarker = new Cesium.DrawHandler(this.viewer, Cesium.DrawMode.Marker)
    this.handlerMarker.activeEvt.addEventListener((isActive) => {
      if (isActive) {
        this.viewer.enableCursorStyle = false
        this.viewer._element.style.cursor = ''
        document.documentElement.style.cursor = 'pointer'
      } else {
        this.viewer.enableCursorStyle = true
        document.documentElement.style.cursor = ''
      }
    })
    this.handlerMarker.movingEvt.addEventListener(({ x, y }) => {
      // 绘制过程中的提示框，根据返回的屏幕坐标设置
      
    })

    this.handlerMarker.drawEvt.addEventListener((result) => {
      // 绘制结束（不是关闭绘制）
      this.markerPoints = result.object.positions
    })
  }

  // 开始绘制
  steatDrawMarker() {
    this.handlerMarker.deactivate() // 先触发一次关闭绘制
    this.handlerMarker.clear() // 先清除一次之前的点位
    this.handlerMarker.activate() // 触发绘制
  }

  // 清除已存在的点位 会同时关闭当前存在的绘制
  clearDrawMarker() {
    this.handlerMarker.clear()
  }

  // 关闭绘制
  endDrawMarker() {
    this.handlerMarker.deactivate()
  }

  /**
   * 添加Marker
   * @param {string} id 
   * @param {string} name 
   * @param {object} position 世界坐标对象 {x,y,z}
   * @returns 
   */
  drawMarkerHandler(id:string,name:string,position:{}) {
    let marker = this.MarkerSource.entities.add({
      id,
      name,
      position,
      label: {
        //文字标签
        text: name,
        font: '14px',
        fillColor: Cesium.Color.RED,
        outlineColor: Cesium.Color.BLACK,
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        outlineWidth: 2,
        showBackground: true,
        backgroundColor: new Cesium.Color(1, 1, 1, 0.5),
        backgroundPadding: new Cesium.Cartesian2(7, 5),
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM, //垂直方向以底部来计算标签的位置
        pixelOffset: new Cesium.Cartesian2(50, 0), //偏移量
      },
      point: {
        color: Cesium.Color.RED, //点位颜色
        pixelSize: 0, //像素点大小
      },
      billboard: {
        //图标
        image: '/public/icon_xds.png',
        width: 20,
        height: 20,
        scale: 1,
        pixelOffset: new Cesium.Cartesian2(0, -12),
      },
    })

    return marker
  }
}

export default DrawMarker;